package org.dromara.school.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.enums.UserType;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.tenant.helper.TenantHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.school.domain.vo.CampusVo;
import org.dromara.school.domain.bo.CampusBo;
import org.dromara.school.service.ICampusService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 校区管理
 *
 * @author gjb
 * @date 2025-05-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/school/campus")
public class CampusController extends BaseController {

    private final ICampusService campusService;

    /**
     * 查询校区管理列表
     */
    @SaCheckPermission("school:campus:list")
    @GetMapping("/list")
    public TableDataInfo<CampusVo> list(CampusBo bo, PageQuery pageQuery) {

        return campusService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出校区管理列表
     */
    @SaCheckPermission("school:campus:export")
    @Log(title = "校区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CampusBo bo, HttpServletResponse response) {
        List<CampusVo> list = campusService.queryList(bo);
        ExcelUtil.exportExcel(list, "校区管理", CampusVo.class, response);
    }

    /**
     * 获取校区管理详细信息
     *
     * @param campusId 主键
     */
    @SaCheckPermission("school:campus:query")
    @GetMapping("/{campusId}")
    public R<CampusVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long campusId) {
        return R.ok(campusService.queryById(campusId));
    }

    /**
     * 新增校区管理
     */
    @SaCheckPermission("school:campus:add")
    @Log(title = "校区管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CampusBo bo) {
        return toAjax(campusService.insertByBo(bo));
    }

    /**
     * 修改校区管理
     */
    @SaCheckPermission("school:campus:edit")
    @Log(title = "校区管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CampusBo bo) {
        return toAjax(campusService.updateByBo(bo));
    }

    /**
     * 删除校区管理
     *
     * @param campusIds 主键串
     */
    @SaCheckPermission("school:campus:remove")
    @Log(title = "校区管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{campusIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] campusIds) {
        return toAjax(campusService.deleteWithValidByIds(List.of(campusIds), true));
    }


    @GetMapping("/getCampusList")
    public R<List<CampusVo>> getcampusByTenantId() {

        return R.ok(campusService.findCampusByTenantId());
    }


    /**
     * 获取所有班级节点
     * @return
     */
    @GetMapping(value = "/getSchoolTreeNode")
    public R getSchoolTreeNode(){

        return R.ok(campusService.getSchoolTreeNode());
    }



}
